package com.rbac.admin.controller.system;

import lombok.RequiredArgsConstructor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 仅用于示例预览的只读 SQL 执行（限制为 SELECT）
 */
@RestController
@RequestMapping("/sql")
@RequiredArgsConstructor
public class SqlPreviewController {

    private final JdbcTemplate jdbcTemplate;

    @PostMapping("/preview")
    public Map<String, Object> preview(@RequestBody Map<String, String> body) {
        String sql = body.getOrDefault("sql", "");
        Map<String, Object> res = new HashMap<>();
        if (sql == null || !sql.trim().toLowerCase().startsWith("select")) {
            res.put("code", 400);
            res.put("message", "Only SELECT is allowed");
            res.put("data", List.of());
            return res;
        }
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        res.put("code", 200);
        res.put("message", "success");
        res.put("data", list);
        return res;
    }
}


